using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace MinaspetroLibrary.Configuracoes
{
    public class Configuracao
    {
        private Configuracao() { }

        public static string GetPastaDeArquivos
        {
            get
            {
                return ConfigurationManager.AppSettings["PastaDeArquivos"].ToString();
            }
        }

        public static string GetDominio
        {
            get
            {
                return ConfigurationManager.AppSettings["Dominio"].ToString();
            }
        }

        public static string GetPastaImagensFull
        {
            get
            {
                return ConfigurationManager.AppSettings["Dominio"].ToString() + ConfigurationManager.AppSettings["PastaDeImagens"].ToString();
            }
        }

        public static string DateFormat24
        {
            get { return "yyyy-MM-dd HH:mm:ss"; }
        }

        public static string DateFormat24Visualizar
        {
            get { return "dd/MM/yyyy HH:mm:ss"; }
        }

        public static string DateFormatShort
        {
            get { return "dd/MM/yyyy"; }
        }

        public static string DateFormat12
        {
            get { return "yyyy-MM-dd hh:mm:ss"; }
        }

        public static int GetNumeroDePaginasNoGrupo
        {
            get
            {
                try
                {
                    string num = ConfigurationManager.AppSettings["NumeroDePaginasNoGrupo"].ToString();
                    return Convert.ToInt32(num);
                }
                catch { return 5; }
            }
        }

        public static int GetNumeroDeItensNaPagina
        {
            get
            {
                try
                {
                    string num = ConfigurationManager.AppSettings["NumeroDeItensNaPagina"].ToString();
                    return Convert.ToInt32(num);
                }
                catch { return 5; }
            }
        }

        public static string UrlPrincipal
        {
            get
            {
                return ConfigurationManager.AppSettings["UrlPrincipal"].ToString();
            }
        }

        public static string ReplaceDimensao(string mapa, int width, int height)
        {
            string widEncontrado = GetWidth(mapa);
            string heiEncontrado = GetHeight(mapa);
            if (widEncontrado != string.Empty && heiEncontrado != string.Empty)
                return mapa.ToLower().Replace(widEncontrado, "width=\"" + width.ToString() + "\"").Replace(heiEncontrado, "height=\"" + height.ToString() + "\"");
            return string.Empty;
        }

        private static string GetWidth(string mapa)
        {
            string width = string.Empty;
            if (mapa.ToLower().Contains("width"))
            {
                int aspas = 0;
                for (int index = mapa.ToLower().IndexOf("width"); index < mapa.Length && aspas < 2; index++)
                {
                    width += mapa[index].ToString();
                    if (mapa[index].ToString() == "\"")
                        aspas++;
                }
            }
            return width;
        }

        private static string GetHeight(string mapa)
        {
            string height = string.Empty;
            if (mapa.ToLower().Contains("height"))
            {
                int aspas = 0;
                for (int index = mapa.ToLower().IndexOf("height"); index < mapa.Length && aspas < 2; index++)
                {
                    height += mapa[index].ToString();
                    if (mapa[index].ToString() == "\"")
                        aspas++;
                }
            }
            return height;
        }
    }
}
